package cn.topcodes.tcsf.app.web.controller.report;

import cn.topcode.unicorn.utils.DateUtil;
import cn.topcodes.tcsf.admin.commons.api.domain.ApiResponseEntity;
import cn.topcodes.tcsf.app.domain.report.ContinuousVo;
import cn.topcodes.tcsf.app.domain.report.PieVo;
import cn.topcodes.tcsf.app.mapper.AppClientMapper;
import cn.topcodes.tcsf.app.service.report.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by yvan
 * Author: Yvan
 * Date: 2017/12/12
 * Time: 下午4:28
 **/
@Controller("appReportController")
@RequestMapping("/admin/report")
public class ReportController {

    @Autowired
    private ReportService reportService;

    @Autowired
    private AppClientMapper appClientMapper;

    @RequestMapping("onlineAccounts")
    public String onlineAccounts(Model model) {

        List<String> allAppInfo = appClientMapper.findAllAppInfo();
        model.addAttribute("apps", allAppInfo);
        model.addAttribute("endDate", DateUtil.format(new Date(), DateUtil.YYYY_MM_DD));
        model.addAttribute("titleEndDate", DateUtil.format(new Date(), "MM月dd号"));
        model.addAttribute("startDate", DateUtil.format(oneMonthAgo(), DateUtil.YYYY_MM_DD));
        model.addAttribute("titleStartDate", DateUtil.format(oneMonthAgo(), "MM月dd号"));
        return "report/onlineAccounts";
    }

    @RequestMapping("getOnlineAccountsCount")
    @ResponseBody
    public ApiResponseEntity getOnlineAccountsCount(@RequestParam(value = "startTime", required = false) String startTime,
                                                    @RequestParam(value = "endTime", required = false) String endTime,
                                                    @RequestParam(value = "appType", required = false) String type) {

        PieVo onlineAccounts = reportService.getOnlineAccounts(startTime, endTime, type);
        return ApiResponseEntity.buildOK().setBody(onlineAccounts);
    }

    @RequestMapping("newAccounts")
    public String newAccounts(Model model) {
        model.addAttribute("endDate", DateUtil.format(new Date(), DateUtil.YYYY_MM_DD));
        model.addAttribute("titleEndDate", DateUtil.format(new Date(), "MM月dd号"));
        model.addAttribute("startDate", DateUtil.format(oneMonthAgo(), DateUtil.YYYY_MM_DD));
        model.addAttribute("titleStartDate", DateUtil.format(oneMonthAgo(), "MM月dd号"));
        return "report/newAccounts";
    }

    @RequestMapping("getNewAccountsCount")
    @ResponseBody
    public ApiResponseEntity getNewAccountsCount(HttpServletResponse response,
                                                 @RequestParam(value = "startTime", required = false) String startTime,
                                                 @RequestParam(value = "endTime", required = false) String endTime) {

        ContinuousVo newAccounts = reportService.getNewAccounts(startTime, endTime);
        return ApiResponseEntity.buildOK().setBody(newAccounts);
    }

    @RequestMapping("activeAccounts")
    public String activeAccounts(Model model) {
        model.addAttribute("startDate", DateUtil.format(setDay(new Date(), -6), DateUtil.YYYY_MM_DD));
        model.addAttribute("endDate", DateUtil.format(new Date(), DateUtil.YYYY_MM_DD));
        model.addAttribute("titleStartDate", DateUtil.format(new Date(), "yyyy年MM月dd号"));
        model.addAttribute("titleEndDate", DateUtil.format(new Date(), "yyyy年MM月dd号"));

        return "report/activeAccounts";
    }

    @RequestMapping("getActiveAccounts")
    @ResponseBody
    public ApiResponseEntity getActiveAccounts(HttpServletResponse response,
                                               @RequestParam(value = "startTime", required = true) String startTime,
                                               @RequestParam(value = "endTime", required = true) String endTime,
                                               @RequestParam(value = "type", required = true) String type) {

        ContinuousVo newAccounts = reportService.getUserActiveCount(startTime, endTime, type);
        return ApiResponseEntity.buildOK().setBody(newAccounts);
    }

    @RequestMapping("appInfos")
    public String appInfos(Model model) {

        List<String> allAppInfo = appClientMapper.findAllAppInfo();
        model.addAttribute("apps", allAppInfo);
        return "report/appInfos";
    }

    @RequestMapping("getAppInfos")
    @ResponseBody
    public ApiResponseEntity getAppInfos(HttpServletResponse response,
                                         @RequestParam(value = "type", required = true) String type) {

        List<PieVo> appInfo = reportService.getAppInfo(type);
        return ApiResponseEntity.buildOK().setBody(appInfo);
    }

    @RequestMapping("userRetention")
    public String userRetention(Model model) {
        model.addAttribute("startDate", DateUtil.format(setDay(new Date(), -6), DateUtil.YYYY_MM_DD));
        model.addAttribute("endDate", DateUtil.format(new Date(), DateUtil.YYYY_MM_DD));
        model.addAttribute("titleStartDate", DateUtil.format(new Date(), "yyyy年MM月dd号"));
        model.addAttribute("titleEndDate", DateUtil.format(new Date(), "yyyy年MM月dd号"));

        return "report/userRetention";
    }

    @RequestMapping("getUserRetention")
    @ResponseBody
    public ApiResponseEntity getUserRetention(HttpServletResponse response,
                                               @RequestParam(value = "startTime", required = true) String startTime,
                                               @RequestParam(value = "endTime", required = true) String endTime,
                                               @RequestParam(value = "type", required = true) String type) {

        ContinuousVo newAccounts = reportService.getUserRetention(startTime, endTime, type);
        return ApiResponseEntity.buildOK().setBody(newAccounts);
    }

    public Date oneMonthAgo() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.set(Calendar.DATE, c.get(Calendar.DATE) - 30);
        return c.getTime();
    }

    /**
     * 设置天数 正数增加 负数减少 单位为天
     *
     * @param time
     * @param days
     * @return
     */
    public Date setDay(Date time, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(Calendar.DATE, days);// 今天 +1
        return calendar.getTime();
    }
}
